using System;
using System.Linq;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using LS.UnityExtensions.Editor;
using LS.Localization;
using Type = System.Type;

namespace LS.Editor.Localization
{
    [CustomEditor(typeof(LanguageSetting))]
    public class LanguageSettingInspector : InspectorBase
    {
        private static readonly Type s_LocalizationSettingType = typeof(LocalizationSetting);

        private string m_NewFileName = "Dictionary";
        private SerializedProperty m_Language;
        private SerializedProperty m_Datas;

        private LocalizationSetting m_Localization;

        class Contents
        {
            public static GUIContent ReferencedBy = new GUIContent("Referenced By");
            public static GUIContent CreateDictionary = new GUIContent("Create New Dictionary");
            public static GUIContent CreateName = new GUIContent("Dictionary Name");
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            m_Language = serializedObject.FindProperty("m_Language");
            m_Datas = serializedObject.FindProperty("m_Datas");
            SearchReferencedLocalizationSetting();
        }

        public override void DrawInspectorItems()
        {
            this.serializedObject.Update();

            using (new EditorGUI.DisabledScope(true))
            {
                EditorGUILayout.ObjectField(Contents.ReferencedBy, m_Localization, s_LocalizationSettingType, false);
                EditorGUILayout.PropertyField(m_Language);
            }
            EditorGUILayout.PropertyField(m_Datas);

            GUILayout.Space(40);
            m_NewFileName = EditorGUILayout.TextField(Contents.CreateName,m_NewFileName);

            if (GUILayout.Button(Contents.CreateDictionary))
            {
                CreateDictionaryFile();
            }
        }

        public override void DrawInspectorFinish()
        {
            base.DrawInspectorFinish();
            


            AssetDatabase.SaveAssetIfDirty(target);
        }

        public void CreateDictionaryFile()
        {
            LanguageSetting setting = target as LanguageSetting;
            string path = AssetDatabase.GetAssetPath(this.target);
            string xmlPath = $"{Path.GetDirectoryName(path)}/{m_NewFileName}.xml";
            TextAsset dic = LocalizationSettingUtil.CreateDirectory(xmlPath);
            int index = m_Datas.arraySize;
            m_Datas.arraySize += 1;
            var property = m_Datas.GetArrayElementAtIndex(index);
            property.objectReferenceValue = dic;
        }

        private void SearchReferencedLocalizationSetting()
        {
            var localization = LocalizationSettingUtil.GetAllProjectAssets<LocalizationSetting>().FirstOrDefault();
            if (localization.Collection.FindIndex(c=>c.Setting == (target as LanguageSetting)) >= 0)
            {
                m_Localization = localization;
            }
        }
    }
}
